package com.zsw.dubbo.console;

import java.lang.reflect.InvocationTargetException;

import javax.xml.bind.PropertyException;

import com.zsw.dubbo.util.StringUtil;

/**
 * 控制台指令对象(指令构建对象)
 * 
 * @author daze
 * 
 */
public class Console {
	public static String FGF_ML = " ";
	public static String FGF_ARG = " ";
	public static String FGF_C_CLASS2METHOD = ".";
	public static String FGF_C_METHOD2ARG = ":";

	private String consoleAll;// 全部指令

	private String console;// 命令
	private String proValue;// pro对应执行命令

	private String[] args;// 参数
	private StringBuffer sargs;
	@SuppressWarnings("rawtypes")
	private Class[] classs;

	private ConsoleB cb;

	public Console(String allConsole) {
		this.consoleAll = allConsole;
		String[] strs = allConsole.split(FGF_ML);
		console = strs[0];
		initArgs(strs);
	}

	/**
	 * 将字符串格式的指令转换为ConsoleB对象
	 * 
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws NoSuchFieldException
	 * @throws PropertyException
	 */
	public synchronized ConsoleB analyze() throws SecurityException,
			NoSuchMethodException, NoSuchFieldException, PropertyException {
		if (cb == null) {
			Object obj = SysConsole.getSysConsole().getPps().get(console);
			if (ConsoleB.class.getName().equals(obj.getClass().getName())) {
				cb = (ConsoleB) obj;
			} else {
				this.proValue = (String) SysConsole.getSysConsole().getPps()
						.get(console);
				if (StringUtil.stringProving(proValue)) {// &&!console.equals(proValue)
					int x = proValue.lastIndexOf(FGF_C_CLASS2METHOD);
					// int y = proValue.lastIndexOf(FGF_C_METHOD2ARG);
					// y = y == -1 ? proValue.length() : y;
					if (x != -1) {
						cb = new ConsoleB(proValue.substring(0, x),
								proValue.substring(x + 1), classs);
					} else {
						throw new PropertyException(StringUtil.strAdd(console,
								" ", StringUtil.ALIGN_LEFT, 16)
								+ "ERROR:"
								+ "(" + proValue + ") 命令解析错误");
					}
				} else {
					throw new NullPointerException("未找到命令!");
				}
			}
		}
		return cb;
	}

	private void initArgs(String[] strs) {
		sargs = new StringBuffer();
		args = new String[strs.length - 1];
		classs = new Class[args.length];
		for (int i = 1; i < strs.length; i++) {
			String s = strs[i];
			if (s.startsWith(FGF_ARG)) {
				s.replaceFirst(FGF_ARG, "");
			}
			args[i - 1] = s;
			classs[i - 1] = String.class;
			sargs.append(s).append(",");
		}
	}

	public Object sendMethod() throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException,
			SecurityException, NoSuchMethodException {
		Object obj = cb.send(null, args);
		return obj;
	}

	public String getProValue() {
		return proValue;
	}

	public void setProValue(String proValue) {
		this.proValue = proValue;
	}

	@SuppressWarnings("rawtypes")
	public Class[] getClasss() {
		return classs;
	}

	@SuppressWarnings("rawtypes")
	public void setClasss(Class[] classs) {
		this.classs = classs;
	}

	public String getConsole() {
		return console;
	}

	public void setConsole(String console) {
		this.console = console;
	}

	public String[] getArgs() {
		return args;
	}

	public void setArgs(String[] args) {
		this.args = args;
	}

	public StringBuffer getSargs() {
		return sargs;
	}

	public void setSargs(StringBuffer sargs) {
		this.sargs = sargs;
	}

	public String getConsoleAll() {
		return consoleAll;
	}

	public void setConsoleAll(String consoleAll) {
		this.consoleAll = consoleAll;
	}

	public ConsoleB getCb() {
		return cb;
	}

	public void setCb(ConsoleB cb) {
		this.cb = cb;
	}

}
